﻿using Masa.BuildingBlocks.Data.UoW;
using Masa.Contrib.Ddd.Domain.Repository.EFCore;
using Masa.CqrsProject.Contexts;
using Masa.CqrsProject.Models;
using Microsoft.EntityFrameworkCore;

namespace Masa.CqrsProject.Repositorys
{

    /// <summary>
    /// 订单查询仓储
    /// </summary>
    public class OrderReadRepository : Repository<CqrsReadDbContext, OrderInfo, int>, IOrderReadRepository
    {

        private readonly CqrsReadDbContext _myDbContext;
        private readonly IUnitOfWork _unitOfWork;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="unitOfWork"></param>
        public OrderReadRepository(CqrsReadDbContext context, IUnitOfWork unitOfWork) : base(context, unitOfWork)
        {
            _myDbContext = context;
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 查询订单
        /// </summary>
        /// <param name="orderWhere"></param>·
        /// <returns></returns>
        public async Task<OrderInfo> GetOrderAsync(string orderWhere)
        {
            //使用导航查询将关联的OrderItems也查询出来
            Console.WriteLine("查询订单...");
            var order = _myDbContext.OrderInfo
                .Include(o => o.OrderItems).ToList()
                .Where((a, b) => a.OrderSn == orderWhere || a.Id.ToString() == orderWhere)
                .FirstOrDefault();
            return order;
        }

        public async Task<List<OrderInfo>> GetOrdersAsync()
        {
            Console.WriteLine("查询订单列表...");

            var list = await _myDbContext.OrderInfo
                .Include(o => o.OrderItems)
                .ToListAsync();
            return list;

        }
    }
}
